package com.djc8.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class BaseDao {    
    public final static String DRIVER = "com.mysql.jdbc.Driver";                 // 鏁版嵁搴撻┍鍔�
    public final static String URL    = "jdbc:mysql://119.29.111.249:3306/schoolgo";   // url
    public final static String DBNAME = "user";                                                           // 鏁版嵁搴撶敤鎴峰悕
    public final static String DBPASS = "2q1a310";                                                           // 鏁版嵁搴撳瘑鐮�
     
    /**
     * 寰楀埌鏁版嵁搴撹繛鎺�
     * @throws ClassNotFoundException
     * @throws SQLException
     * @return 鏁版嵁搴撹繛鎺�
     */
    public Connection getConn() throws ClassNotFoundException, SQLException{
        Class.forName(DRIVER);                                                    //娉ㄥ唽椹卞姩
        Connection conn = DriverManager.getConnection(URL,DBNAME,DBPASS);        //鑾峰緱鏁版嵁搴撹繛鎺�
        return conn ;                                                            //杩斿洖杩炴帴
    }
    
    /**
     * 閲婃斁璧勬簮
     * @param conn 鏁版嵁搴撹繛鎺�
     * @param pstmt PreparedStatement瀵硅薄
     * @param rs 缁撴灉闆�
     */
    public void closeAll( Connection conn, PreparedStatement pstmt, ResultSet rs ) {
        /*  濡傛灉rs涓嶇┖锛屽叧闂璻s  */
        if(rs != null){
            try { rs.close();} catch (SQLException e) {e.printStackTrace();}
        }
        /*  濡傛灉pstmt涓嶇┖锛屽叧闂璸stmt  */
        if(pstmt != null){
            try { pstmt.close();} catch (SQLException e) {e.printStackTrace();}
        }
        /*  濡傛灉conn涓嶇┖锛屽叧闂璫onn  */
        if(conn != null){
            try { conn.close();} catch (SQLException e) {e.printStackTrace();}
        }
    }
    
    /**
     * 鎵цSQL璇彞锛屽彲浠ヨ繘琛屽銆佸垹銆佹敼鐨勬搷浣滐紝涓嶈兘鎵ц鏌ヨ
     * @param sql  棰勭紪璇戠殑 SQL 璇彞
     * @param param  棰勭紪璇戠殑 SQL 璇彞涓殑鈥橈紵鈥欏弬鏁扮殑瀛楃涓叉暟缁�
     * @return 褰卞搷鐨勬潯鏁�
     */
    public int executeSQL(String preparedSql,String[] param) {
        Connection        conn  = null;
        PreparedStatement pstmt = null;
        int               num   = 0;
        
        /*  澶勭悊SQL,鎵цSQL  */
        try {
            conn = getConn();                              // 寰楀埌鏁版嵁搴撹繛鎺�
            pstmt = conn.prepareStatement(preparedSql);    // 寰楀埌PreparedStatement瀵硅薄
            if( param != null ) {
                for( int i = 0; i < param.length; i++ ) {
                    pstmt.setString(i+1, param[i]);         // 涓洪缂栬瘧sql璁剧疆鍙傛暟
                }
            }
            num = pstmt.executeUpdate();                    // 鎵цSQL璇彞
        } catch (ClassNotFoundException e) {
            e.printStackTrace();                            // 澶勭悊ClassNotFoundException寮傚父
        } catch (SQLException e) {
            e.printStackTrace();                            // 澶勭悊SQLException寮傚父
        } finally {
            closeAll(conn,pstmt,null);                     // 閲婃斁璧勬簮
        }
        return num;
    }

    
  
}

